<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Jobs\OrderSubmit;
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Carbon\Carbon;
use App\Repository\UpThree\MainOrderRepostition;

class XinWangController extends BaseController
{
  private $log;
  public function __construct()
  {
    $this->log = new Logger('XinWangPush');
    $this->log->pushHandler(
      (new RotatingFileHandler(storage_path('/logs/XinWangPush.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
    );
  }
  // 电信那边推送过来的数据 
  public function pushOrderStatus(Request $Request)
  {
    $getDataa = file_get_contents('php://input');
    $this->log->info('input' . ($getDataa));
    // return json_encode(['responseCode' => '00000', 'description' => 'success']);
    // die();
    // var_dump(1111111111);
    // $getDataa = '{"cmcc_json_paras":"b902c20bfbf42e7b0b503a98bb78c3cf3f7b0c6c6739fb7d0849f3bbd61313dfd085f5597b3c90c5ad6ff529f239348a4a5bd8c8203afe586a7364c6770dc55c40cdc9c7faa5b5887dfb80d122f4530280b6f33e602262167053170db58243c73a30b4b4fbce12bc039e8916fe24b4f9a48e2df132b2d9402a86e31ab9761cbb88a8d38d15900ae1fdf2fdc34384d52e5d1a2b125b105081dc6e2957faf6a930b96216075bc20eff0836803d10e980a1fa5480734790207a2b356f7b7ac2ae80a80b7d7a865ee2e2139b0bfd33d4aaa4cfc2ce1ead04b505ebad8c0467da66fd52a52d04a4b0d0fb7b68fa604c8e37b6ace5a8cb11675b837a88d9dc36fd0960","method":"orderLogisticsNotify"}';
    // $getData = '{"activeTime":"20230625215603","channelId":"1096","orderNumber":"XW001096202306192007450434421313","orderStatus":"6","outNum":"","phone":"19396373087","productId":"HBDX2955","remark":"\u5df2\u6fc0\u6d3b","transferId":"HK_47668687"}';
    // 数据处理  
    $str1 = json_decode($getDataa, true);
    if (!empty($str1['cmcc_json_paras'])) {
      $str = (hex2bin($str1['cmcc_json_paras']));
      $key = ('yUSZYdeOswVI9mrqxLC6vAJGXNPagjc7');
      $getData = openssl_decrypt($str, 'aes-256-ecb', $key, OPENSSL_RAW_DATA);
    }

    if (empty($getData)) {
      return json_encode(['responseCode' => '00000', 'description' => 'success']);
    }
    $getData = json_decode($getData, true);
    if (empty($getData)) {
      return json_encode(['responseCode' => '00000', 'description' => 'success']);
    }
    // var_dump($getData,111111111111);
    // $returnStatusArr = [
    //   'D1002' => '40', //已激活
    //   'A1003' => '10', // 待处理
    //   'K1008' => '11', //代发货
    //   'K1009' => '20', // 已发货/代签收
    //   'Q1003' => '51', // 订单取消
    //   'D1001' => '30', // 已签收
    // ];
    $returnStatusArr = array(
      'UNDO' => 1,
      'SENT' => 10,
      'MAKE_CARD' => 10,
      'DELIVER' => 20,
      'SIGNED' => 30,
      'COMPLETED' => 40,
      'CANCEL' => 51,
      'NOTPASS' => 50
    );
    // 1、待处理；2、成功；3、失败；4、办理中；5、已发货；6、已激活
    $returnStatusArr = [
      1 => 1,
      2 => 10,
      3 => 50,
      4 => 10,
      5 => 20,
      6 => 40,
      7 => 30
    ];
    // 将数据格式 修改成固定的数据格式
    $editData = [
      'status' => !empty($returnStatusArr[$getData['orderStatus']]) ? $returnStatusArr[$getData['orderStatus']] : '50',
      // 'express_company' => !empty($getData['logisticsCompany']) ? $getData['logisticsCompany'] : '',
      // 'express_number' => !empty($getData['logisticsSingleNumber']) ? $getData['logisticsSingleNumber'] : '',
      'three_msg' => !empty($getData['remark']) ? $getData['remark'] : '',
      'iccid' => !empty($getData['phone']) ? $getData['phone'] : '',
      'first_status' => !empty($getData['balanceOne']) ? '1' : '0',
      'first_status_desc' => !empty($getData['balanceOne']) ? ($getData['balanceOne'] / 100) : '0',
    ];
    if(!empty($getData['activeTime'])){
      $editData['activat_at'] = date('Y-m-d H:i:s',strtotime($getData['activeTime']));
    }
    if (!empty($getData['logisticsCompany'])) {
      $editData['express_company'] = $getData['logisticsCompany'];
      $editData['express_number'] = $getData['logisticsSingleNumber'];
    }
    $where = ['ordersno' => $getData['transferId']];
    $model = new MainOrderRepostition();
    $res = $model->followOrderStatus($where, $editData);
    $this->log->info('新网通知参数' . json_encode($getData) . '返回参数' . json_encode($res));
    if (!empty($res['code']) && $res['code'] == 200) {
      return json_encode(['responseCode' => '00000', 'description' => 'success']);
    }
    return json_encode(['responseCode' => '00000', 'description' => 'success']);
  }
}
